/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.stemaini.utility;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 *
 * @author Administrator
 */
public class BacaDataFile {
    
    
    koneksi_1 akses = new koneksi_1();
    int i=0;
    String Judul;
    String Isi;

    public BacaDataFile()
    {
        Judul = null;
        Isi = null;
    }



    private String RemovePetik(String Str){

        String temp="";

        for(int j=0;j<Str.length();j++)
        {
            if(Str.charAt(j) != '\'')
            {
                temp = temp + Str.charAt(j);
            }
        }

        return temp;
    }

    public void BacaFolder(String pathFolder, boolean def) throws FileNotFoundException, IOException{
        String dir="";
        if(def){
            dir = "C:/Documents and Settings/Administrator/My Documents/data/";
        }
        else{
            dir = pathFolder;
        }
        File directory = new File(dir);
        //Koneksi
        koneksi_1 dbAkses = new koneksi_1();

        //Operasi
        File files[] = directory.listFiles();
        for (File f : files) {
            BacaFile(dir+f.getName().toString());
        }

    }

    public void BacaFile(String pathFile)
    {
        File file = new File(pathFile);

        int ch;
        StringBuffer strContent = new StringBuffer("");
        FileInputStream fin = null;

        try
        {
          fin = new FileInputStream(file);

          while( (ch = fin.read()) != -1)
            strContent.append((char)ch);
          fin.close();
        }
        catch(FileNotFoundException e)
        {
          System.out.println("File " + file.getAbsolutePath() +
                                 " could not be found on filesystem");
        }
        catch(IOException ioe)
        {
          System.out.println("Exception while reading the file" + ioe);
        }
        
        HTMLParser baru;
        baru = new HTMLParser(strContent.toString());
        //menghilangkan tag-tag yang tidak terpakai di judul dan isi berita
        baru.stripTag("p");
        baru.stripTag("strong");
        baru.stripTag("style ");
        baru.stripTag("br ");

        Judul = RemovePetik(baru.getTagContents("title"));
        Isi = RemovePetik(baru.getTagContents("isi_data"));

        //insert data ke dalam data base
        //akses.insertTbData_Korpus(Judul, Isi);
    }
}
